home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Development / 3DMF parser / 1.0 version / MF3DPC / MFBINRD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-07  |  6.7 KB  |  234 lines  |  [TEXT/dosa]

  1. /*==============================================================================
  2.  *
  3.  *    File:        MFBINRD.C
  4.  *
  5.  *    Function:    Miscellaneous functions needed during binary reads.
  6.  *
  7.  *    Version:    Metafile:    Version 1.0 3DMF files
  8.  *                Package:    Release #2 of this code
  9.  *
  10.  *    Author(s):    Rick Wong (RWW), Duet Development Corp.
  11.  *                John Kelly (JRK), Duet Development Corp.
  12.  *
  13.  *    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  14.  *
  15.  *    Change History (most recent first):
  16.  *        Fabio    Changed file name to 8 characters
  17.  *        F3A_RWW    Binary TOC.
  18.  *        F2O_RWW    File created.
  19.  *==============================================================================
  20.  */
  21.  
  22. #include "MFBINRD.H"
  23.  
  24. #include <string.h>            /* memcpy */
  25.  
  26. #include "MF3D.H"
  27. #include "MFINT64.H"
  28. #include "MFERRORS.H"
  29. #include "MFMACROS.H"
  30. #include "MFMEMORY.H"
  31. #include "MFOBJECT.H"
  32.  
  33. #if defined(applec) || defined(__MWERKS__) || defined(THINK_C)
  34. #pragma segment __MF3D__
  35. #endif
  36.  
  37. MF3DErr
  38. MF3D_BinaryReadProc(
  39.     MF3D_FilePtr        inMetafilePtr,
  40.     MF3DSize            inAmtToRead,
  41.     char *                outBuffer)
  42. {
  43.     if ((inMetafilePtr->readBuffer.bufPos + inAmtToRead) >
  44.             inMetafilePtr->readBuffer.bufSize)
  45.     {    return kMF3DErrCantParse;
  46.     }
  47.  
  48.     memcpy(outBuffer,
  49.             &inMetafilePtr->readBuffer.buf[inMetafilePtr->readBuffer.bufPos],
  50.             inAmtToRead);
  51.     inMetafilePtr->readBuffer.bufPos += inAmtToRead;
  52.  
  53.     return kMF3DNoErr;
  54. }
  55.  
  56. /*==============================================================================
  57.  *    MF3D_PreprocessBinaryFile
  58.  *
  59.  *    Read TOC into memory.
  60.  *    First, we read the metafile header [NOTE: This is inefficient because it is
  61.  *        also the first thing we will do when the next GetAnObject is called].
  62.  *    Then, we will read in the Table of Contents objects, building the tocStuff
  63.  *        table.
  64.  *==============================================================================
  65.  */
  66. MF3DErr
  67. MF3D_PreprocessBinaryFile(
  68.     MF3D_FilePtr        inMetafilePtr)
  69. {
  70.     MF3DMetafileObjPtr            headerPtr;
  71.     MF3DTableOfContentsObjPtr    tocPtr;
  72.     MF3DBinaryFilePosition        nextTocLoc;
  73.     MF3DUns32                    numReferences;
  74.     MF3DErr                        result;
  75.  
  76.     numReferences = 0;
  77.     /* tocStuff.numReferences must be initialized before calling IntReadObj */
  78.     inMetafilePtr->tocStuff.numReferences = 0;
  79.     inMetafilePtr->tocStuff.references = MF3D_Malloc(0);
  80.     inMetafilePtr->tocStuff.refSeed = 1;
  81.     inMetafilePtr->tocStuff.typeSeed = -1;
  82.  
  83.     /* Note: MF3D_IntReadObject sets headerPtr to NULL if it fails
  84.      * (so it is okay to call dispose later)
  85.      */
  86.     result = MF3D_IntReadObject(inMetafilePtr, (MF3DVoidObjPtr *)&headerPtr);
  87.  
  88.     if (result == kMF3DNoErr)
  89.     {    if (headerPtr->objectType != kMF3DObjMetafile)
  90.             result = kMF3DErrCantParse;    /* first obj is not metafile header */
  91.     }
  92.  
  93.     MFASSERT(result != kMF3DNoErr ||
  94.             !MF3DIsTextFormat(headerPtr->tocLocation->format));
  95.     
  96.     /* if we have read a good file header, set file read position    */
  97.     /* to read the first item in the table of contents                 */
  98.     if (result == kMF3DNoErr)
  99.     {        AssignInt64(nextTocLoc, headerPtr->tocLocation->location.binary);
  100.     }
  101.  
  102.     /* If we have a table of contents */
  103.     while (result == kMF3DNoErr && (nextTocLoc.hi != 0 || nextTocLoc.lo != 0))
  104.     {    MF3D_TOCReferencePtr    refPtr;
  105.         MF3DUns32                numNewRefs;
  106.  
  107.         tocPtr = NULL;
  108.  
  109.         result = MF3DSeekPosition(inMetafilePtr, nextTocLoc);
  110.  
  111.         if (result == kMF3DNoErr)
  112.         {    result = MF3D_IntReadObject(inMetafilePtr,
  113.                     (MF3DVoidObjPtr *)&tocPtr);
  114.         }
  115.  
  116.         if (result == kMF3DNoErr)
  117.         {    if (tocPtr->objectType != kMF3DObjTableOfContents)
  118.                 result = kMF3DErrCantParse;
  119.         }
  120.  
  121.         if (result == kMF3DNoErr)
  122.         {    MFASSERT(!MF3DIsTextFormat(tocPtr->nextTOC->format));
  123.             AssignInt64(nextTocLoc, tocPtr->nextTOC->location.binary);
  124.             numNewRefs = tocPtr->nEntries;
  125.             refPtr = MF3D_Realloc(inMetafilePtr->tocStuff.references,
  126.                     ((numReferences + numNewRefs) * sizeof(*refPtr)));
  127.             if (refPtr == NULL)
  128.             {    MF3D_Free(inMetafilePtr->tocStuff.references);
  129.                 result = kMF3DErrOutOfMemory;
  130.             }
  131.         }
  132.  
  133.         if (result == kMF3DNoErr)
  134.         {    MF3D_TOCEntryPtr    tocRefPtr;
  135.  
  136.             inMetafilePtr->tocStuff.references = refPtr;
  137.             refPtr = &inMetafilePtr->tocStuff.references[numReferences - 1];
  138.             numReferences += numNewRefs;
  139.  
  140.             if (tocPtr->refSeed > inMetafilePtr->tocStuff.refSeed)
  141.                 inMetafilePtr->tocStuff.refSeed = tocPtr->refSeed;
  142.             if (tocPtr->typeSeed < inMetafilePtr->tocStuff.typeSeed)
  143.                 inMetafilePtr->tocStuff.typeSeed = tocPtr->typeSeed;
  144.             tocRefPtr = tocPtr->tocEntries;
  145.             while (numNewRefs > 0)
  146.             {    refPtr->refID = tocRefPtr->refID;
  147.                 MFASSERT(!MF3DIsTextFormat(tocRefPtr->objLocation->format));
  148.                 AssignInt64(refPtr->ref.location,
  149.                         tocRefPtr->objLocation->location.binary);
  150.                 ++refPtr;
  151.                 ++tocRefPtr;
  152.                 --numNewRefs;
  153.             }
  154.             inMetafilePtr->tocStuff.numReferences = numReferences;
  155.         }
  156.  
  157.         MF3DDisposeObject((MF3DVoidObjPtr)tocPtr);
  158.     }
  159.  
  160.     MF3DDisposeObject((MF3DVoidObjPtr)headerPtr);
  161.  
  162.     if (result == kMF3DNoErr)
  163.     {    /* Reset file back to zero to start real reading */
  164.         MF3DBinaryFilePosition    zero;
  165.  
  166.         SetInt64ToZero(zero);
  167.         result = MF3DSeekPosition(inMetafilePtr, zero);
  168.     }
  169.  
  170.     return result;
  171. }
  172.  
  173. /*==============================================================================
  174.  *    MF3D_GetRefNameB
  175.  *
  176.  *    No reference names yet for binary files
  177.  *    @@@But maybe we will want to create them?
  178.  *==============================================================================
  179.  */
  180. MF3DCStringPtr
  181. MF3D_GetRefNameB(
  182.     MF3D_FilePtr        inMetafilePtr,
  183.     MF3DReferenceID        inRefID)
  184. {
  185.     MF3D_Unused(inMetafilePtr);
  186.     MF3D_Unused(inRefID);
  187.     return NULL;
  188. }
  189.  
  190. /*==============================================================================
  191.  *    MF3D_GetBinaryRefID
  192.  *
  193.  *    Return the refID associated with inLocation, 0 if none.
  194.  *==============================================================================
  195.  */
  196. MF3DErr
  197. MF3D_GetBinaryRefID(
  198.     MF3D_FilePtr            inMetafilePtr,
  199.     MF3DBinaryFilePosition    inLocation,
  200.     MF3DReferenceID            *outRefID)
  201. {
  202.     MF3D_TOCReferencePtr    refPtr;
  203.     MF3DUns32                refCount;
  204.  
  205.     *outRefID = 0;
  206.     refCount = inMetafilePtr->tocStuff.numReferences;
  207.     refPtr = inMetafilePtr->tocStuff.references;
  208.     while (refCount > 0)
  209.     {    if (CompareInt64(refPtr->ref.location, inLocation) == 0)
  210.         {    *outRefID = refPtr->refID;
  211.             break;            /* ### LOOP EXIT if reference found */
  212.         }
  213.         --refCount;
  214.         ++refPtr;
  215.     }
  216.  
  217.     return kMF3DNoErr;        /* for now, always no err, even if no match */
  218. }
  219.  
  220. /*==============================================================================
  221.  *    MF3D_PostprocessBinaryFile
  222.  *
  223.  *    Dispose of TOC structures
  224.  *==============================================================================
  225.  */
  226. MF3DErr
  227. MF3D_PostprocessBinaryFile(
  228.     MF3D_FilePtr        inMetafilePtr)
  229. {
  230.     MF3D_Free(inMetafilePtr->tocStuff.references);
  231.     return kMF3DNoErr;
  232. }
  233.  
  234.